<?php

namespace app\common\model;

class Order extends Common
{
	protected $insert = ['dateline' => SYS_TIME];
	protected function getAlldataAttr($value, $data)
	{
		$paytype = config('PAYTYPE');
		$status = [0 => '未支付', 1 => '支付成功', 2 => '支付失败'];
		$uinfo = model('AdminUser')->field('userid,username,realname,dealerid,puserid')->where('userid', $data['userid'])->find();
		$data['realname'] = $uinfo['realname'] ?: $uinfo['username'];
		$data['dealname'] = model('AdminUser')->where('userid', $uinfo['dealerid'])->value('realname');
		$data['payname'] = isset($paytype[$data['paytype']]) ? $paytype[$data['paytype']] : '--';
		$data['statusname'] = isset($status[$data['status']]) ? $status[$data['status']] : '--';
		$data['paydate'] = $data['paytime'] ? date('Y-m-d H:i:s', $data['paytime']) : '未支付';
		$data['dateline'] = date('Y-m-d H:i:s', $data['dateline']);
		return $data;
	}
	public function pay_success($ordersn, $price, $paytype = 0)
	{
		$info = $this->where('ordersn', $ordersn)->find();
		if ($info['status'] >= 1) {
			return false;
		}
		if (strpos($ordersn, 'activate') !== false) { // 如果当前订单是激活优惠券的订单
			$mcid = model('Order')->where('ordersn', $ordersn)->value('m_cid');
			$time = SYS_TIME;
			model('MemberCoupons')->where('id', $mcid)->update(['isactivate' => 1, 'activatetime' => $time]);
			model('OrderVip')->where('ordersn', $ordersn)->update(['status' => 1, 'paytime' => $time]);
			model('Order')->where('ordersn', $ordersn)->update(['status' => 1, 'paytime' => $time]);
			//
			$cinfo = model('MemberCoupons')->where('id', $mcid)->find();
			if (isset($cinfo['ownerid']) && $cinfo['ownerid']) {
				model('MemberCoupons')->where('ownerid', $cinfo['ownerid'])->where('userid', 'neq', $cinfo['userid'])->update(['status' => 2]);
			}
			return true;
		} else {
			foreach (config('ordertype') as $k => $v) {
				if (strpos($ordersn, $k) !== false) {
					model($v['model'])->pay_success($ordersn, $info['userid'], $price, $paytype);
					break;
				}
			}
			if ($info['paytype'] == 0) {
				return $this->where('ordersn', $ordersn)->update(['status' => 1, 'paytime' => SYS_TIME, 'paytype' => $paytype]);
			} else {
				return $this->where('ordersn', $ordersn)->update(['status' => 1, 'paytime' => SYS_TIME]);
			}
		}
	}
	public function pay_fail($ordersn, $price, $reason = '')
	{
		$info = $this->where('ordersn', $ordersn)->find();
		if ($info['status'] == -1) {
			return false;
		}

		foreach (config('ordertype') as $k => $v) {
			if (strpos($ordersn, $k) !== false) {
				model($v['model'])->pay_fail($ordersn, $info['userid'], $price, $reason);
				break;
			}
		}
		return $this->where('ordersn', $ordersn)->update(['status' => 2, 'paytime' => SYS_TIME]);
	}
	protected function getNicknameAttr($value, $data)
	{
		$info = model("Member")->get_data(['item_id' => $data['userid']]);
		return $info['nickname'];
	}
	protected function getPaynameAttr($value, $data)
	{
		return config('PAYTYPE.' . $data['paytype']);
	}
	protected function getStatusnameAttr($value, $data)
	{
		$status = [0 => '未支付', 1 => '支付成功', 2 => '支付失败'];
		return $status[$data['status']];
	}
	protected function getPaytimeAttr($value, $data)
	{
		return $value ? date('Y-m-d H:i:s', $value) : '未支付';
	}
	protected function getDatelineAttr($value, $data)
	{
		return date('Y-m-d H:i:s', $value);
	}
}
